define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/send/index' + location.search,
                    add_url: 'coupon/send/add',
                    //edit_url: 'coupon/send/edit',
                    del_url: 'coupon/send/del',
                    multi_url: 'coupon/send/multi',
                    import_url: 'coupon/send/import',
                    table: 'coupon_send',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'coupon_id', title: __('Coupon_id')},
                        {field: 'coupon.name', title: __('Coupon.name'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'sendtime', title: __('Sendtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'num', title: __('Num')},
                        {field: 'status', title: __('发放状态'), searchList: {"0":__('未发放'),"1":__('已发放')}, formatter: Table.api.formatter.normal},
                        //{field: 'user_id', title: __('User_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {
                            field: 'operate', title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    text: __('查看发送用户'),
                                    icon: '',
                                    refresh:true,
                                    classname: 'btn btn-info btn-xs',
                                    url: 'user/coupon?ids={id}',
                                    extend:' target="_blank"',
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
